//
//  NewsController.m
//  WY
//
//  Created by lanou3g on 16/5/9.
//  Copyright © 2016年 pcc. All rights reserved.
//

#import "NewsController.h"
#import "NewsCell.h"
#import "NewsModel.h"
#import "WebViewController.h"
#import "LORefresh.h"
@interface NewsController ()
@property (nonatomic, strong) NSMutableArray *newsArray;
//刷新个数
@property (nonatomic, assign) NSInteger start;
//头部帅刷新视图
@property (nonatomic, strong) LORefresh *header;
//@property (nonatomic,strong) lor
@end

@implementation NewsController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    [self loadDataWithStart:0];

    
    self.header = [self.tableView gifHeader];
    [self.header setGifName:@"wangnima.jpg"];
    //下拉刷新
    __weak typeof(self) weakSelf = self;
    [self.tableView addRefreshWithRefreshViewType:LORefreshViewTypeHeaderGif refreshingBlock:^{
        _start = weakSelf.newsArray.count;
        [weakSelf.newsArray  removeAllObjects];
        [weakSelf.tableView reloadData];
        [weakSelf loadDataWithStart:_start];
    }];
    
    //下拉加载
    [self.tableView addRefreshWithRefreshViewType:LORefreshViewTypeFooterDefault refreshingBlock:^{
        _start += 10;
        [weakSelf loadDataWithStart:_start];
        
        
    }];
  
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

#warning Bug上拉加载都会刷新,self.newsArray.count保持不变why
- (void)loadDataWithStart:(NSInteger )start {
    NSLog(@"append to news channel == %@,_start = %ld",self.channelId,self.start);
    [NewsModel newsDatasWithURL:[NSString stringWithFormat:@"article/headline/%@/%ld-20.html",self.channelId,_start] success:^(NSArray *news) {
        self.newsArray = (NSMutableArray *) news;
 
         [self.tableView reloadData];
        
        NSLog(@"%ld",self.newsArray.count);
        //停止刷新
        [[self.tableView gifHeader] endRefreshing];
        //停止加载
        [[self.tableView defaultFooter] endRefreshing];
        
        
    }];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return self.newsArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NewsModel *newsModel = self.newsArray[indexPath.row];
    NewsCell *cell = [tableView dequeueReusableCellWithIdentifier:[NewsCell cellIdentiferWithNews:newsModel] forIndexPath:indexPath];
    
    // Configure the cell...
    cell.newsModel = newsModel;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NewsModel *model = self.newsArray[indexPath.row];
    return [NewsCell cellHeightWithNews:model];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    WebViewController *webVC = [[WebViewController alloc]init];
    NewsModel *model = self.newsArray[indexPath.row];
    webVC.model = model;
    [self.navigationController pushViewController:webVC animated:YES];
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
